package com.yoomiito.app.ui.anyview.order;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.yoomiito.app.R;
import com.yoomiito.app.interf.BaseOrderGoodsView;
import com.yoomiito.app.model.bean.AppInterfaceInfo;
import com.yoomiito.app.model.bean.GoodsDetail;
import com.yoomiito.app.model.my.MyRemainMoneyInfo;
import com.yoomiito.app.utils.AllUtils;
import com.yoomiito.app.utils.DensityUtil;
import com.yoomiito.app.utils.ImageLoader;
import com.yoomiito.app.utils.L;
import com.yoomiito.app.utils.SpannableUtils;
import com.yoomiito.app.utils.YMToast;
import com.yoomiito.app.widget.NumControlView;

/**
 * 确认订单------商品信息 数量 优米抵扣
 */
public class YouPinOrderGoodsView extends BaseOrderGoodsView {

    private DeductionView deductionView;
    private int goodsSize = 1;
    public MyRemainMoneyInfo mMoneyInfo;
    private String mGoodsPrice;//用于计算商品价格

    public YouPinOrderGoodsView(Context context, GoodsDetail detail, AppInterfaceInfo appInterfaceInfo) {
        super(context, detail,appInterfaceInfo);
    }

    @Override
    public int getView() {
        return R.layout.view_youpin_order_goods;
    }

    @Override
    public void initView() {
        ImageView goodsIv = findViewById(R.id.item_order_goods_iv);
        TextView goodsTitle = findViewById(R.id.item_order_goods_title);
        TextView goodsPrice = findViewById(R.id.item_order_goods_price);
        NumControlView numControlView = findViewById(R.id.num_control_view);

        FrameLayout frameLayout = findViewById(R.id.frameLayout);
        frameLayout.setVisibility(View.VISIBLE);
        deductionView = new DeductionView(mContext);
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.topMargin = DensityUtil.dp2px(12);
        frameLayout.addView(deductionView, lp);

        ImageLoader.getInstance().into(mContext, mGoodsDetail.getPict_url(), goodsIv);
        goodsTitle.setText(mGoodsDetail.getTitle());
        goodsPrice.setText("¥" + mGoodsDetail.getSalePrice());
        numControlView.setListener(num -> {
            goodsSize = num;
            changeMoney();
        });
        numControlView.addOnInputNumChangeListener(num -> {
            goodsSize = num;
            changeMoney();
        });
    }

    private void changeMoney() {
        int youMoneyPay = 0;
        String youmiSize = AllUtils.priceMul_2(mGoodsDetail.getDeduction_account() , goodsSize);

        if(SpannableUtils.isEmptyOr0(mGoodsDetail.getDeduction_account())){
            mGoodsPrice = mGoodsDetail.getSalePrice();//使用商品原价作为价格计算
            youMoneyPay = 0;
            youmiSize = "0";
        }else if (AllUtils.moneyCompare(mMoneyInfo.getDeduction_account(), youmiSize)) {//大于等于
//            mGoodsPrice = mGoodsDetail.getCash();//使用商品扣除优米后的价格作为价格计算
            mGoodsPrice = AllUtils.priceSub_2(mGoodsDetail.getSalePrice(),mGoodsDetail.getDeduction_account());
            youMoneyPay = 1;
        } else {//小于
            mGoodsPrice = mGoodsDetail.getSalePrice();//使用商品原价作为价格计算
            youMoneyPay = 0;
            youmiSize = "0";
            YMToast.showText("可抵扣余额不足,无法抵扣");
        }
        calculatePrice(youmiSize, youMoneyPay);
    }

    private void calculatePrice(String youmiSize, int youMoneyPay) {
        deductionView.setValue(youmiSize);
//        mGoodsTotalPrice = AllUtils.priceSub(AllUtils.priceMul_2(mGoodsPrice, goodsSize), String.valueOf(youmiSize));
        L.i("计算付款：" + mGoodsPrice + "  " + goodsSize);
        mGoodsTotalPrice = AllUtils.priceMul_2(mGoodsPrice, goodsSize);
        L.i("计算付款后的值：" + mGoodsTotalPrice);
        String s = "共" + goodsSize + "件 合计¥" + mGoodsTotalPrice;
        int p = s.indexOf("¥");
        if (mOnShowInfoListener != null) {
            mOnShowInfoListener.onShow(SpannableUtils.getSizeAndPriceText(s, R.color.color_FF035B, p, s.length(), p, p + 1), goodsSize, youMoneyPay);
        }
    }

    @Override
    public void setMoneyInfo(MyRemainMoneyInfo moneyInfo) {
        mMoneyInfo = moneyInfo;
        if (SpannableUtils.isEmptyOr0(mGoodsDetail.getDeduction_account())) {//如果抵扣优米为0 则不显示
            deductionView.setVisibility(GONE);
        } else {
            deductionView.setVisibility(VISIBLE);
            deductionView.setYoumiSize(moneyInfo.getDeduction_account());
        }
        int youMoneyPay;
        String youmiSize = mGoodsDetail.getDeduction_account();
        if(SpannableUtils.isEmptyOr0(mGoodsDetail.getDeduction_account())){
            mGoodsPrice = mGoodsDetail.getSalePrice();//使用商品原价作为价格计算
            youMoneyPay = 0;
            youmiSize = "0";
        }else if (AllUtils.moneyCompare(mMoneyInfo.getDeduction_account(), mGoodsDetail.getDeduction_account())) {//大于等于
//            mGoodsPrice = mGoodsDetail.getCash();//使用商品扣除优米后的价格作为价格计算
            mGoodsPrice = AllUtils.priceSub_2(mGoodsDetail.getSalePrice(),mGoodsDetail.getDeduction_account());
            youMoneyPay = 1;
        } else {//小于
            mGoodsPrice = mGoodsDetail.getSalePrice();//使用商品原价作为价格计算
            youMoneyPay = 0;
            youmiSize = "0";
        }
        calculatePrice(youmiSize, youMoneyPay);
    }
}
